using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Resources;
namespace MicroMsg.Scene
{
	public class NetSceneEmojiUploadImage : NetSceneBaseEx<UploadEmojiRequest, UploadEmojiResponse, UploadEmojiRequest.Builder>
	{
		private const string TAG = "NetSceneEmojiUploadImage";
		private const int ONE_2GPACK_UPLOADSIZE = 8192;
		private const int ONE_3GPACK_UPLOADSIZE = 16384;
		private const int ONE_WIFIPACK_UPLOADSIZE = 32768;
		private int mUpLoadedSize = 8192;
		private EmojiInfo mEmojiInfo;
		private ChatMsg mChatMsgInfo;
		private MemoryStream sendBufStream;
		public static EmojiInfoMap emojiInfoMap_0;
		private int nDoSceneCount;
		public NetSceneEmojiUploadImage()
		{
			EventCenter.registerEventHandler(EventConst.ON_ACCOUNT_LOGIN, new EventHandlerDelegate(NetSceneEmojiUploadImage.onAccountLoginProc));
		}
		public static void onAccountLoginProc(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			NetSceneEmojiUploadImage.fixSendEmojiStatus();
		}
		private static void fixSendEmojiStatus()
		{
			List<ChatMsg> list = StorageMgr.chatMsg.searhCacheMsg(47, 0, 1);
			using (List<ChatMsg>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					current.nStatus = 1;
				}
			}
			StorageMgr.chatMsg.updateMsgList(list);
		}
		public bool reSendEmoji(ChatMsg chatMsg)
		{
			if (NetSceneEmojiUploadImage.emojiInfoMap_0 == null)
			{
				NetSceneEmojiUploadImage.emojiInfoMap_0 = new EmojiInfoMap();
			}
			if (NetSceneEmojiUploadImage.emojiInfoMap_0.sendingInfoHash.ContainsKey(chatMsg.strClientMsgId))
			{
				Log.smethod_1("NetSceneEmojiUploadImage", "the msg has already in sending map");
				return false;
			}
			if (chatMsg != null && chatMsg.nMsgType == 47 && chatMsg.nStatus == 1 && !string.IsNullOrEmpty(chatMsg.strPath) && !string.IsNullOrEmpty(chatMsg.strMsg))
			{
				this.mChatMsgInfo = chatMsg;
				bool result;
				try
				{
					if (!chatMsg.strPath.StartsWith("Resource") && !chatMsg.strPath.StartsWith("/Resource"))
					{
						byte[] array = StorageIO.readFromFile(chatMsg.strPath);
						this.sendBufStream = new MemoryStream(array);
					}
					else
					{
						Uri uri = new Uri(chatMsg.strPath, 2);
						StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
						if (resourceStream == null)
						{
							result = false;
							return result;
						}
						byte[] array2 = new byte[resourceStream.get_Stream().get_Length()];
						int num = resourceStream.get_Stream().Read(array2, 0, (int)resourceStream.get_Stream().get_Length());
						if (num <= 0)
						{
							result = false;
							return result;
						}
						this.sendBufStream = new MemoryStream(array2);
					}
					this.mEmojiInfo = EmojiImageService.getEmojiInfo(this.mChatMsgInfo.strMsg);
					this.mEmojiInfo.strClientID = this.mChatMsgInfo.strClientMsgId;
					this.mEmojiInfo.nChatMsgID = this.mChatMsgInfo.nMsgLocalID;
					this.mEmojiInfo.isFirstTime = true;
					if (NetSceneEmojiUploadImage.emojiInfoMap_0 == null)
					{
						NetSceneEmojiUploadImage.emojiInfoMap_0 = new EmojiInfoMap();
					}
					NetSceneEmojiUploadImage.emojiInfoMap_0.sendingInfoHash.Add(this.mEmojiInfo.strClientID, this.mEmojiInfo);
					try
					{
						this.mUpLoadedSize = this.getSendPackSize();
					}
					catch (Exception ex)
					{
						Log.smethod_1("NetSceneEmojiUploadImage", ex.ToString());
						this.mUpLoadedSize = 32768;
					}
					this.mChatMsgInfo.nStatus = 0;
					StorageMgr.chatMsg.updateMsg(this.mChatMsgInfo);
					result = this.doScene();
				}
				catch (Exception ex2)
				{
					Log.smethod_1("NetSceneEmojiUploadImage", ex2.get_StackTrace());
					result = false;
				}
				return result;
			}
			return false;
		}
		public bool doScene(EmojiSendParam param, int msgType = 47)
		{
			if (param == null || string.IsNullOrEmpty(param.talkerName))
			{
				return false;
			}
			if (param.origStream == null && param.gameExtInfo == null)
			{
				return false;
			}
			if (param.origStream != null && string.IsNullOrEmpty(param.strMd5))
			{
				return false;
			}
			string hashString = MD5Core.GetHashString(param.thumbPath + param.talkerName + Util.getNowMilliseconds());
			string text = "";
			if (param.origStream != null)
			{
				text = this.copyImg(param, hashString);
				if (string.IsNullOrEmpty(text) || this.sendBufStream == null || this.sendBufStream.get_Length() <= 0L)
				{
					return false;
				}
			}
			this.mChatMsgInfo = new ChatMsg();
			this.mChatMsgInfo.strTalker = param.talkerName;
			this.mChatMsgInfo.nMsgType = msgType;
			this.mChatMsgInfo.nStatus = 0;
			this.mChatMsgInfo.nIsSender = 1;
			this.mChatMsgInfo.nCreateTime = (long)(Util.getNowMilliseconds() / 1000.0);
			this.mChatMsgInfo.strClientMsgId = hashString;
			this.mChatMsgInfo.strThumbnail = param.thumbPath;
			this.mChatMsgInfo.strPath = param.filePath;
			this.mEmojiInfo = new EmojiInfo();
			this.mEmojiInfo.nChatMsgID = this.mChatMsgInfo.nMsgLocalID;
			this.mEmojiInfo.nStartPos = 0;
			if (param.origStream != null)
			{
				this.mEmojiInfo.nTotalLen = (int)param.origStream.get_Length();
			}
			else
			{
				this.mEmojiInfo.nTotalLen = 11765;
			}
			this.mEmojiInfo.nMsgType = this.mChatMsgInfo.nMsgType;
			this.mEmojiInfo.strClientID = this.mChatMsgInfo.strClientMsgId;
			if (string.IsNullOrEmpty(param.strMd5))
			{
				this.mEmojiInfo.strMd5 = text;
			}
			else
			{
				this.mEmojiInfo.strMd5 = param.strMd5;
			}
			this.mEmojiInfo.toUserName = this.mChatMsgInfo.strTalker;
			this.mEmojiInfo.isFirstTime = true;
			this.mEmojiInfo.nImgType = param.nImgType;
			this.mEmojiInfo.gameExtInfo = param.gameExtInfo;
			this.mChatMsgInfo.strMsg = EmojiImageService.getEmojiInfoXml(this.mEmojiInfo);
			StorageMgr.chatMsg.addMsg(this.mChatMsgInfo);
			if (NetSceneEmojiUploadImage.emojiInfoMap_0 == null)
			{
				NetSceneEmojiUploadImage.emojiInfoMap_0 = new EmojiInfoMap();
			}
			NetSceneEmojiUploadImage.emojiInfoMap_0.sendingInfoHash.Add(this.mEmojiInfo.strClientID, this.mEmojiInfo);
			try
			{
				this.mUpLoadedSize = this.getSendPackSize();
			}
			catch (Exception ex)
			{
				Log.smethod_1("NetSceneEmojiUploadImage", ex.ToString());
				this.mUpLoadedSize = 32768;
			}
			return this.doScene();
		}
		public string copyImg(EmojiSendParam param, string strFileName)
		{
			byte[] array = new byte[param.origStream.get_Length()];
			int num = param.origStream.Read(array, 0, (int)param.origStream.get_Length());
			if (num <= 0)
			{
				return null;
			}
			if (this.sendBufStream == null)
			{
				this.sendBufStream = new MemoryStream();
			}
			this.sendBufStream.Seek(0L, 0);
			this.sendBufStream.Write(array, 0, num);
			return MD5Core.GetHashString(array);
		}
		private byte[] readFromStream(int startPos, int dataLen)
		{
			byte[] result;
			try
			{
				if (this.sendBufStream != null && (long)dataLen <= this.sendBufStream.get_Length())
				{
					byte[] array = new byte[dataLen];
					this.sendBufStream.Seek((long)startPos, 0);
					int num = this.sendBufStream.Read(array, 0, dataLen);
					if (num <= 0)
					{
						Log.smethod_1("NetSceneEmojiUploadImage", "read from stream lenght = " + num);
						result = null;
					}
					else
					{
						Log.smethod_5("NetSceneEmojiUploadImage", "read length " + num);
						result = array;
					}
				}
				else
				{
					result = null;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("NetSceneEmojiUploadImage", ex.get_Message());
				result = null;
			}
			return result;
		}
		private bool doScene()
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			EmojiUploadInfoReq.Builder builder = new EmojiUploadInfoReq.Builder();
			if (this.mEmojiInfo.isFirstTime)
			{
				builder.ClientMsgID = this.mEmojiInfo.strClientID;
				builder.StartPos = 0;
				builder.TotalLen = this.mEmojiInfo.nTotalLen;
				builder.ToUserName = this.mEmojiInfo.toUserName;
				builder.Type = this.mEmojiInfo.nImgType;
				builder.MD5 = this.mEmojiInfo.strMd5;
				builder.EmojiBuffer = Util.toSKBuffer(new byte[0]);
				string gameExtInfoXml = EmojiImageService.getGameExtInfoXml(this.mEmojiInfo.gameExtInfo);
				if (gameExtInfoXml != null)
				{
					builder.ExternXML = gameExtInfoXml;
				}
				else
				{
					builder.ExternXML = "";
				}
				builder.Report = "";
				EmojiUploadInfoReq emojiUploadInfoReq = builder.Build();
				this.mBuilder.EmojiItemList.Add(emojiUploadInfoReq);
				this.mBuilder.SetEmojiItemCount(this.mBuilder.EmojiItemList.get_Count());
			}
			else
			{
				builder.ClientMsgID = this.mEmojiInfo.strClientID;
				builder.StartPos = this.mEmojiInfo.nStartPos;
				builder.TotalLen = this.mEmojiInfo.nTotalLen;
				builder.ToUserName = this.mEmojiInfo.toUserName;
				builder.Type = this.mEmojiInfo.nImgType;
				int dataLen = (this.mEmojiInfo.nTotalLen - this.mEmojiInfo.nStartPos < this.mUpLoadedSize) ? (this.mEmojiInfo.nTotalLen - this.mEmojiInfo.nStartPos) : this.mUpLoadedSize;
				byte[] array = this.readFromStream(this.mEmojiInfo.nStartPos, dataLen);
				if (array == null)
				{
					Log.smethod_1("NetSceneEmojiUploadImage", "send buffer has no data ");
					return false;
				}
				builder.EmojiBuffer = Util.toSKBuffer(array);
				builder.MD5 = this.mEmojiInfo.strMd5;
				string gameExtInfoXml2 = EmojiImageService.getGameExtInfoXml(this.mEmojiInfo.gameExtInfo);
				if (gameExtInfoXml2 != null)
				{
					builder.ExternXML = gameExtInfoXml2;
				}
				else
				{
					builder.ExternXML = "";
				}
				builder.Report = "";
				EmojiUploadInfoReq emojiUploadInfoReq2 = builder.Build();
				this.mBuilder.EmojiItemList.Add(emojiUploadInfoReq2);
				this.mBuilder.SetEmojiItemCount(this.mBuilder.EmojiItemList.get_Count());
			}
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/sendemoji";
			this.mSessionPack.mNeedCompress = false;
			this.mSessionPack.mConnectMode = 2;
			base.endBuilder();
			this.nDoSceneCount++;
			return true;
		}
		protected override void onFailed(UploadEmojiRequest request, UploadEmojiResponse response)
		{
			Log.smethod_1("NetSceneEmojiUploadImage", "Send image failed because of system error");
			NetSceneEmojiUploadImage.emojiInfoMap_0.sendingInfoHash.Remove(this.mEmojiInfo.strClientID);
			this.mChatMsgInfo.nStatus = 1;
			StorageMgr.chatMsg.updateMsg(this.mChatMsgInfo);
		}
		protected override void onSuccess(UploadEmojiRequest request, UploadEmojiResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			EmojiUploadInfoResp emojiUploadInfoResp = response.EmojiItemList.get_Item(0);
			if (ret != RetConst.MM_OK || emojiUploadInfoResp.Ret != 0)
			{
				Log.smethod_1("NetSceneEmojiUploadImage", "Send image failed because of net error ret = " + ret.ToString());
				NetSceneEmojiUploadImage.emojiInfoMap_0.sendingInfoHash.Remove(this.mEmojiInfo.strClientID);
				this.mChatMsgInfo.nStatus = 1;
				StorageMgr.chatMsg.updateMsg(this.mChatMsgInfo);
				return;
			}
			if (emojiUploadInfoResp.TotalLen <= 0)
			{
				Log.smethod_1("NetSceneEmojiUploadImage", "resp error resp.TotalLen <= 0");
				return;
			}
			if (this.mEmojiInfo.strMd5 == emojiUploadInfoResp.MD5)
			{
				this.mEmojiInfo.isFirstTime = false;
				this.mEmojiInfo.nStartPos = emojiUploadInfoResp.StartPos;
				if (emojiUploadInfoResp.MsgID > 0u)
				{
					this.mEmojiInfo.nSvrID = (int)emojiUploadInfoResp.MsgID;
					this.mChatMsgInfo.nMsgSvrID = this.mEmojiInfo.nSvrID;
				}
			}
			if (this.isUploadCompleted())
			{
				Log.smethod_4("NetSceneEmojiUploadImage", "send Complete!");
				NetSceneEmojiUploadImage.emojiInfoMap_0.sendingInfoHash.Remove(this.mEmojiInfo.strClientID);
				this.mChatMsgInfo.nStatus = 2;
				StorageMgr.chatMsg.updateMsg(this.mChatMsgInfo);
				return;
			}
			if (this.nDoSceneCount <= 20)
			{
				this.doScene();
			}
		}
		private bool isUploadCompleted()
		{
			return this.mEmojiInfo.nStartPos >= this.mEmojiInfo.nTotalLen;
		}
		public void doSceneFinished()
		{
			Log.smethod_4("NetSceneEmojiUploadImage", "netscene do finished. ");
		}
		public int getSendPackSize()
		{
			if (NetworkDeviceWatcher.isCurrentWifi())
			{
				Log.smethod_4("NetSceneEmojiUploadImage", "Current net is Wifi");
				return 32768;
			}
			if (NetworkDeviceWatcher.isCurrentCell3G())
			{
				Log.smethod_4("NetSceneEmojiUploadImage", "Current net is 3G");
				return 16384;
			}
			if (NetworkDeviceWatcher.isCurrentCell2G())
			{
				Log.smethod_4("NetSceneEmojiUploadImage", "Current net is 2G");
				return 8192;
			}
			Log.smethod_4("NetSceneEmojiUploadImage", "Current net is UnKnow");
			return 16384;
		}
	}
}
